--
-- Author: mm
-- Date: 2018-05-07 18:07:08
--
-- GameplaypreviewView
--

local string_find = string.find
local string_split = string.split

local panelTypeNum = 3 -- 页面类型个数 = level_preview.pageType

local GameplaypreviewBigRender = import(".renders.GameplaypreviewBigRender")
local GameplaypreviewMidRender = import(".renders.GameplaypreviewMidRender")
local GameplaypreviewSmlRender = import(".renders.GameplaypreviewSmlRender")

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ViewBase)

ClassRef.VIEW_ROOT_RES = Res.GamePlayPreviewUi

function ClassRef:init()
	self._ui.Button_close:addClickEventListener(function()
		executeFunc(self._onCloseView)
	end)
	-- 左切页
	self._ui.Button_1:addClickEventListener(function()
		executeFunc(self._onOperate, "goto_leftPage")
	end)
	-- 右切页
	self._ui.Button_2:addClickEventListener(function()
		executeFunc(self._onOperate, "goto_rightPage")
	end)

	self._ui.tlt:setString( string.format( L("lua_code_text_3"), UD:getLevel() ) )

	self._dataTemp = nil -- 延迟初始化期间缓存的数据
	self._panelTempList = {}
	self:performWithDelay( handler( self, self._delayInit ) , 0 )
end

-- 延迟初始化
function ClassRef:_delayInit()
	for i = 1 , panelTypeNum do
		local panelList = self:_preCreatePanel( i )
		for _, v in ipairs( panelList ) do
			self._ui.Panel_content:addChild( v )
		end
		self._panelTempList[ i ] = panelList
	end
	-- 初始化完成
	self._initEnd =  true
	if self._dataTemp then -- 存在数据？
		self:refreshContent( self._dataTemp )
		self._dataTemp = nil
	end
end
-- 预创建panel
function ClassRef:_preCreatePanel( panelType )
	local temp = {}
	if 1 == panelType then -- 1分页
		local panelTemp = GameplaypreviewBigRender.create()
		panelTemp:setData()
		table.insert( temp, panelTemp )
	elseif 2 == panelType then -- 2分页
		local panelTemp_1 = GameplaypreviewMidRender.create()
		panelTemp_1:setData()
		table.insert( temp, panelTemp_1 )

		local panelTemp_2 = GameplaypreviewSmlRender.create()
		panelTemp_2:setData()
		table.insert( temp, panelTemp_2 )
	elseif 3 == panelType then -- 3分页
		local panelTemp_1 = GameplaypreviewSmlRender.create()
		panelTemp_1:setData()
		table.insert( temp, panelTemp_1 )

		local panelTemp_2 = GameplaypreviewSmlRender.create()
		panelTemp_2:setData()
		table.insert( temp, panelTemp_2 )

		local panelTemp_3 = GameplaypreviewSmlRender.create()
		panelTemp_3:setData()
		table.insert( temp, panelTemp_3 )
	end
	return temp
end

-- level_previewConf
function ClassRef:refreshContent( conf )
	if not self._initEnd then -- 未初始化完成
		-- print( " ------------>>>>> GameplaypreviewView:refreshContent 未初始化完成" )
		self._dataTemp = conf
		return
	end

	-- 1=1/1整块
	-- 2=1/2整块
	-- 3=1/3整块
	-- 4=2/3整块
	local pageList = string_split(  conf.pageType, ";" )
	if #pageList > panelTypeNum then
		print( " ------------>>>>> GameplaypreviewView 页面子panel个数超过定义的类型个数" )
		return
	end

	-- 隐藏全部panel可见性
	for i,v in ipairs( self._panelTempList ) do
		for i2,v2 in ipairs( v ) do
			v2:setVisible( false )
		end
	end
	-- 显示当前配置对应的页面格式
	local listTemp = self._panelTempList[#pageList]
	for i,v in ipairs( listTemp ) do
		v:setVisible( true )
	end
	-- 调整当前页面中子panel的posX
	self:_adjustPanelPosX( pageList, listTemp )

	-- 设置具体内容

	-- 正常顺序赋值辅助函数
	local function setDataFunc()
		for i,v in ipairs( listTemp ) do
			local dataTemp = nil
			if conf["content" .. i] and 0 ~= tonumber( conf["content" .. i] ) then -- 存在具体内容
				dataTemp = {}
				dataTemp.content = tonumber( conf["content" .. i] )
				dataTemp.unlock = conf["unlock" .. i]
				dataTemp.value = conf["value" .. i] or ""
				dataTemp.funcBg = conf["funcBg" .. i] or ""
				dataTemp.tips = conf["tips" .. i] or ""
				dataTemp.finish = conf["finish" .. i] or ""
				dataTemp.title = conf["title" .. i] or ""
				dataTemp.slogan = conf["slogan" .. i] or ""
			end
			v:setData( dataTemp )
		end
	end
	if 2 == #listTemp then -- page分成2部分的需要动态调整复制顺序
		if 3 == tonumber( pageList[1] ) then -- 1/3分页在前？需要逆序赋值
			for i,v in ipairs( listTemp ) do
				local dataTemp = nil
				local idx = ( #listTemp + 1 - i )
				if conf["content" .. idx ] and 0 ~= tonumber( conf["content" .. idx ] ) then -- 存在具体内容
					dataTemp = {}
					dataTemp.content = tonumber( conf["content" .. idx] )
					dataTemp.unlock = conf["unlock" .. idx]
					dataTemp.value = conf["value" .. idx] or ""
					dataTemp.funcBg = conf["funcBg" .. idx] or ""
					dataTemp.tips = conf["tips" .. idx] or ""
					dataTemp.finish = conf["finish" .. idx] or ""
					dataTemp.title = conf["title" .. idx] or ""
					dataTemp.slogan = conf["slogan" .. idx] or ""
				end
				v:setData( dataTemp )
			end
		else
			setDataFunc()
		end
	else
		setDataFunc()
	end
end

function ClassRef:_adjustPanelPosX( pageList, listTemp )
	if #pageList ~= #listTemp then
		print( " -------------->>> #pageList ~= #listTemp " )
		return
	end

	local panelNum = #pageList
	local startPosX = 0
	if 1 == panelNum then -- 1分页
		listTemp[1]:setPositionX( startPosX )
	elseif 2 == panelNum then -- 2分页
		if 4 == tonumber( pageList[1] ) then -- 2/3整块在前
			listTemp[1]:setPositionX( startPosX )
			local widthTemp = listTemp[1]:getContentSize2()
			listTemp[2]:setPositionX( startPosX + widthTemp )
		else
			listTemp[2]:setPositionX( startPosX )
			local widthTemp = listTemp[2]:getContentSize2()
			listTemp[1]:setPositionX( startPosX + widthTemp )
		end
	elseif 3 == panelNum then -- 3分页
		listTemp[1]:setPositionX( startPosX )
		local widthTemp = listTemp[1]:getContentSize2()
		listTemp[2]:setPositionX( startPosX + widthTemp )
		listTemp[3]:setPositionX( startPosX + 2*widthTemp )
	end
end

-- 设置左切按钮可见性
function ClassRef:setLeftBtnVisible( b )
	self._ui.Button_1:setVisible( b )
end
-- 设置右切按钮可见性
function ClassRef:setRightBtnVisible( b )
	self._ui.Button_2:setVisible( b )
end


return ClassRef
